package com.dgj.log.module;

import javax.servlet.http.HttpSession;

import org.nutz.lang.Strings;
import org.nutz.mvc.View;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.IocBy;
import org.nutz.mvc.annotation.Modules;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Views;
import org.nutz.mvc.ioc.provider.ComboIocProvider;

import com.dgj.DefaultDESKey;
import com.dgj.biz.vo.VO;
import com.dgj.nutz.captcha.JPEGView;
import com.dgj.nutz.module.base.AbstractBaseModule;
import com.dgj.nutz.velocity.VelocityViewMaker;
import com.dgj.utils.encode.DES;
import com.dgj.utils.entries.Result;

/**
 * @author 崔海军 create at 2015年7月9日 上午10:31:07
 */
@Modules(scanPackage = true)
@IocBy(type = ComboIocProvider.class, args = { "*org.nutz.ioc.loader.json.JsonLoader", "conf", "*org.nutz.ioc.loader.annotation.AnnotationIocLoader", "com.dgj" })
@Ok("json")
@Views({ VelocityViewMaker.class })
public class MainModule extends AbstractBaseModule {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.dgj.nutz.module.base.AbstractBaseModule#_getNameSpace()
	 */
	@Override
	public String _getNameSpace() {
		return null;
	}

	@At("/")
	@Ok("jsp:/login")
	public Result login(HttpSession session) {
		String dgj = _getCookie("log");
		String userName = null;
		String password = null;
		boolean rememberMe = false;
		if (!Strings.isBlank(dgj)) {
			String info = DES.decrypt(dgj, DefaultDESKey.getKey());
			userName = info.split(":")[0];
			password = info.split(":")[1];
			rememberMe = true;
		}
		if (!rememberMe) {
			_addCookie("log", DES.encrypt(userName + ":" + password, DefaultDESKey.getKey()), 0);
		}
		return Result.success().addData("userName", userName).addData("password", password).addData("rememberMe", rememberMe ? "checked" : "").addData("flag", rememberMe);
	}

	@At
	public View captcha() {
		return new JPEGView(null, 4);
	}

	@At("/main")
	@Ok("vel:layout/main.vm")
	public Result vel(HttpSession session) {
		return Result.success().addData("session", session.getAttribute(VO.SystemConst.USER_SESSION_KEY.getConfigValue()));
	}
}
